package com.ruoyi.kong.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.enums.YesOrNo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.kong.bean.NextStepBean;
import com.ruoyi.kong.bean.StepBeanData;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.kong.mapper.KStepMapper;
import com.ruoyi.kong.domain.KStep;
import com.ruoyi.kong.service.IKStepService;

import static com.ruoyi.common.utils.SecurityUtils.getUserId;

/**
 * 命令步骤Service业务层处理
 * 
 * @author zeroUp
 * @date 2024-06-20
 */
@Service
public class KStepServiceImpl implements IKStepService 
{
    @Autowired
    private KStepMapper kStepMapper;

    /**
     * 查询命令步骤
     * 
     * @param gid 命令步骤主键
     * @return 命令步骤
     */
    @Override
    public KStep selectKStepByGid(Long gid)
    {
        return kStepMapper.selectKStepByGid(gid);
    }

    /**
     * 查询命令步骤列表
     * 
     * @param kStep 命令步骤
     * @return 命令步骤
     */
    @Override
    public List<KStep> selectKStepList(KStep kStep)
    {
        kStep.setCreaterBy(getUserId().toString());
        return kStepMapper.selectKStepList(kStep);
    }

    public List<KStep> selectKStepListOb(Long gid)
    {
        KStep kStep = new KStep();
        kStep.setCmdSetGid(gid);
        kStep.setCreaterBy(getUserId().toString());
        List<KStep> list = this.selectKStepList(kStep);
        for (KStep kStep1 : list){
            kStep1.setStepServiceDataObj(JSON.parseObject(kStep1.getStepServiceData(), StepBeanData.class));
        }
        return list;
    }

    public NextStepBean selectNextStepBean(Long gid,Map<String, Object> params){
        List<KStep> list = this.selectKStepListOb(gid);
        Map<Long, KStep> map = list.stream().collect(Collectors.toMap(KStep::getParentGid, Function.identity()));
        Set<Long> keys = map.keySet();
        NextStepBean nextStepBean = new NextStepBean();
        nextStepBean.setOrder(1L);
        this.builderTree(nextStepBean,map,params);
        return nextStepBean;
    }


    public NextStepBean builderTree(NextStepBean root,Map<Long, KStep> map,Map<String, Object> params){
        KStep kStep = map.get(root.getOrder());
        if(kStep==null){
            return null;
        }
        if(StringUtils.isNotBlank(kStep.getStepServiceData())){
            StepBeanData stepBeanData = JSON.parseObject(kStep.getStepServiceData(),StepBeanData.class);
            if(YesOrNo.YES.eq(stepBeanData.getIsFillVal())){
                String value = stepBeanData.getDataValue();
                if(params!=null && StringUtils.isNotBlank(value)){
                    Set<String> keys = params.keySet();
                    for (String key : keys){
                        value = value.replace(key,params.get(key).toString());
                    }
                    stepBeanData.setDataValue(value);
                }

            }else{
                if(params!=null && !params.isEmpty()){
                    stepBeanData.setDataValue(JSON.toJSONString(params));
                }
            }
            if(stepBeanData.getSelectControlIndex()==-2){
                String num = params.get("selectControlIndex").toString();
                if(StringUtils.isNotBlank(num) && StringUtils.isNumeric(num)){
                    stepBeanData.setSelectControlIndex(Integer.valueOf(num));
                }
            }
            root.setServiceData(stepBeanData);
        }
        root.setName(kStep.getControlName());
        root.setDelay(kStep.getDelay());
        root.setStep(kStep.getStep());
        root.setTClass(kStep.getExecuteClassName());
        NextStepBean nextStepBean = new NextStepBean();
        nextStepBean.setOrder(root.getOrder()+1);
        root.setNext(builderTree(nextStepBean,map,params));
       return root;
    }

    /**
     * 新增命令步骤
     * 
     * @param kStep 命令步骤
     * @return 结果
     */
    @Override
    public int insertKStep(KStep kStep)
    {
        kStep.setCreaterBy(SecurityUtils.getUserId().toString());
        return kStepMapper.insertKStep(kStep);
    }

    public int insertKStepAll(List<KStep> kStep){
        for (KStep kStep1 : kStep){
            kStep1.setCreateBy(getUserId().toString());
            kStep1.setStepServiceData(JSON.toJSONString(kStep1.getStepServiceDataObj()));
            if(kStep1.getGid()==null){
                kStepMapper.insertKStep(kStep1);
            }else{
                kStepMapper.updateKStep(kStep1);
            }
        }
        return kStep.size();
    }

    /**
     * 修改命令步骤
     * 
     * @param kStep 命令步骤
     * @return 结果
     */
    @Override
    public int updateKStep(KStep kStep)
    {
        return kStepMapper.updateKStep(kStep);
    }

    /**
     * 批量删除命令步骤
     * 
     * @param gids 需要删除的命令步骤主键
     * @return 结果
     */
    @Override
    public int deleteKStepByGids(Long[] gids)
    {
        return kStepMapper.deleteKStepByGids(gids);
    }

    /**
     * 删除命令步骤信息
     * 
     * @param gid 命令步骤主键
     * @return 结果
     */
    @Override
    public int deleteKStepByGid(Long gid)
    {
        return kStepMapper.deleteKStepByGid(gid);
    }
}
